﻿using CPPEI.ShareScreen.Event.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ShareScreen.App
{
    public partial class MainWindow
    {
        [CPPEI.Coolzon.Common.Subscribe]
        public void OnReceiveUIGetClientListEvent(Event.UI.UIGetClientListEvent @event)
        {
            List<ClientInfo> clientList = new List<ClientInfo>();
            for (int index = 0; index < @event.IdArr.Length; index++)
            {
                clientList.Add(new ClientInfo()
                {
                    Id = @event.IdArr[index],
                    IP = new System.Net.IPAddress(@event.IPArr[index]).ToString(),
                    Port = @event.PortArr[index]
                });
            }

            this.Dispatcher.Invoke(() =>
            {
                this._ctlListClient.ItemsSource = clientList;
            });
        }

        [CPPEI.Coolzon.Common.Subscribe]
        public void OnReceiveUIEnterUploadRDModeEvent(CPPEI.ShareScreen.Event.UI.UIEnterUploadRDModeEvent @event)
        {
            this.Dispatcher.Invoke(() =>
            {
                if (_uploadDeskThread == null)
                {
                    _uploadDeskThread = new RemoteDesk.UploadDeskThread(this._screenCapture);
                }
                _uploadDeskThread.Start();
            });
        }

        [CPPEI.Coolzon.Common.Subscribe]
        public void OnReceiveUIExitUploadRDModeEvent(CPPEI.ShareScreen.Event.UI.UIExitUploadRDModeEvent @event)
        {
            this.Dispatcher.Invoke(() =>
            {
                if (_uploadDeskThread != null)
                {
                    _uploadDeskThread.Stop();
                    _uploadDeskThread = null;
                }
            });
        }

        [CPPEI.Coolzon.Common.Subscribe]
        public void OnReceiveUIRDConnectToClientEvent(UIRDConnectToClientEvent @event)
        {
            this.Dispatcher.Invoke(() =>
            {
                CPPEI.ShareScreen.App.RD.RDWindow window = new RD.RDWindow(_proxy,
                    @event.TargetClientID,
                    @event.RoomID);
                window.Show();
            });
            //this.Visibility = System.Windows.Visibility.Hidden;
        }
    }

    public class ClientInfo
    {
        public uint Id { get; set; }
        public string IP { get; set; }
        public int Port { get; set; }
    }
}
